package com.egghiro.framework.j2se.game.core.view.canvas;

import java.awt.Graphics2D;

import com.egghiro.framework.j2se.util.Stack;

public class Graphics2DStoreStack {
    private final Stack<Graphics2DStore> storeStack = new Stack<Graphics2DStore>();
    /**
	 * Memento @ Memento
	 */
    private final Graphics2D g2d;

    public Graphics2DStoreStack(Graphics2D g2d) {
        this.g2d = g2d;
    }

    public void save() {
        Graphics2DStore g2dStore = new Graphics2DStore();
        g2dStore.save(this.g2d);
        storeStack.push(g2dStore);
    }

    public void restore() {
        if (!storeStack.isEmpty()) {
            Graphics2DStore g2dStore = null;
            if (storeStack.size() == 1) {
                g2dStore = storeStack.peek();
            } else {
                g2dStore = storeStack.pop();
            }
            g2dStore.restore(this.g2d);
        }
    }
}
